package com.haixiaoke.saas.enterprise.apartment.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.haixiaoke.saas.apartment.domain.UnsubscribeRule;
import com.haixiaoke.saas.apartment.service.IUnsubscribeRuleService;
import com.haixiaoke.saas.common.annotation.Log;
import com.haixiaoke.saas.common.annotation.TenantScope;
import com.haixiaoke.saas.common.constant.HouseConstants;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.core.page.TableDataInfo;
import com.haixiaoke.saas.common.enums.BusinessType;
import com.haixiaoke.saas.common.tenant.TenantContext;
import com.haixiaoke.saas.common.utils.poi.ExcelUtil;
import com.haixiaoke.saas.system.BaseController;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 退订规则Controller
 *
 * @author panda
 * @date 2024-03-30
 */
@RestController
@RequestMapping("/enterprise/apartment/unsubscribeRule")
public class EnterpriseUnsubscribeRuleController extends BaseController {
    @Autowired
    private IUnsubscribeRuleService unsubscribeRuleService;

    /**
     * 查询退订规则列表
     */
    @PreAuthorize("@ss.hasPermi('apartment:rule:list')")
    @GetMapping("/page")
    @TenantScope
    public TableDataInfo page(UnsubscribeRule unsubscribeRule) {
        startPage();
        unsubscribeRule.setTenantId(TenantContext.getTenantId());
        List<UnsubscribeRule> list = unsubscribeRuleService.selectUnsubscribeRuleList(unsubscribeRule);
        return getDataTable(list);
    }

    /**
     * 查询退订规则列表
     */
    @GetMapping("/list")
    @TenantScope
    public AjaxResult list(UnsubscribeRule unsubscribeRule) {
        unsubscribeRule.setTenantId(TenantContext.getTenantId());
        unsubscribeRule.setUnsubscribeState(HouseConstants.UNSUBSCRIBE_STATE_SHOW);
        List<UnsubscribeRule> list = unsubscribeRuleService.selectUnsubscribeRuleList(unsubscribeRule);
        return success(list);
    }

    /**
     * 导出退订规则列表
     */
    @PreAuthorize("@ss.hasPermi('apartment:rule:export')")
    @Log(title = "退订规则", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UnsubscribeRule unsubscribeRule) {
        List<UnsubscribeRule> list = unsubscribeRuleService.selectUnsubscribeRuleList(unsubscribeRule);
        ExcelUtil<UnsubscribeRule> util = new ExcelUtil<UnsubscribeRule>(UnsubscribeRule.class);
        util.exportExcel(response, list, "退订规则数据");
    }

    /**
     * 获取退订规则详细信息
     */
    @PreAuthorize("@ss.hasPermi('apartment:rule:query')")
    @GetMapping(value = "/{unsubscribeRuleId}")
    public AjaxResult getInfo(@PathVariable("unsubscribeRuleId") Long unsubscribeRuleId) {
        return success(unsubscribeRuleService.selectUnsubscribeRuleByUnsubscribeRuleId(unsubscribeRuleId));
    }

    /**
     * 新增退订规则
     */
    @PreAuthorize("@ss.hasPermi('apartment:rule:add')")
    @Log(title = "退订规则", businessType = BusinessType.INSERT)
    @PostMapping
    @TenantScope
    public AjaxResult add(@RequestBody UnsubscribeRule unsubscribeRule) {
        unsubscribeRule.setTenantId(TenantContext.getTenantId());
        if (unsubscribeRuleService.checkRuleName(unsubscribeRule)){
            return error("名称已重复");
        }
        unsubscribeRule.setCreateBy(getUsername());
        return toAjax(unsubscribeRuleService.insertUnsubscribeRule(unsubscribeRule));
    }

    /**
     * 修改退订规则
     */
    @PreAuthorize("@ss.hasPermi('apartment:rule:edit')")
    @Log(title = "退订规则", businessType = BusinessType.UPDATE)
    @PutMapping
    @TenantScope
    public AjaxResult edit(@RequestBody UnsubscribeRule unsubscribeRule) {
        unsubscribeRule.setTenantId(TenantContext.getTenantId());
        if (unsubscribeRuleService.checkRuleName(unsubscribeRule)){
            return error("名称已重复");
        }
        unsubscribeRule.setUpdateBy(getUsername());
        return toAjax(unsubscribeRuleService.updateUnsubscribeRule(unsubscribeRule));
    }

    /**
     * 修改退订规则状态
     */
    @PreAuthorize("@ss.hasPermi('apartment:rule:edit')")
    @Log(title = "退订规则", businessType = BusinessType.UPDATE)
    @PutMapping("/edit/state")
    @TenantScope
    public AjaxResult editState(@RequestBody UnsubscribeRule unsubscribeRule) {
        unsubscribeRule.setTenantId(TenantContext.getTenantId());
        unsubscribeRule.setUpdateBy(getUsername());
        return toAjax(unsubscribeRuleService.updateUnsubscribeRuleState(unsubscribeRule));
    }

    /**
     * 删除退订规则
     */
    @PreAuthorize("@ss.hasPermi('apartment:rule:remove')")
    @Log(title = "退订规则", businessType = BusinessType.DELETE)
    @DeleteMapping("/{unsubscribeRuleId}")
    @TenantScope
    public AjaxResult remove(@PathVariable Long unsubscribeRuleId) {
        UnsubscribeRule unsubscribeRule = unsubscribeRuleService.selectUnsubscribeRuleById(unsubscribeRuleId);
        if (!TenantContext.getTenantId().equals(unsubscribeRule.getTenantId())) {
            return warn("数据异常");
        }
        if (unsubscribeRuleService.checkAllocationByRule(unsubscribeRuleId)){
            return warn("退订规则已关联房间，不能删除");
        }
        return toAjax(unsubscribeRuleService.deleteUnsubscribeRuleByUnsubscribeRuleId(unsubscribeRuleId));
    }
}
